<?php

namespace app\controller\wechat;


use app\common\controller\WechatController;
use app\common\model\wechat\WechatCoupon;
use hiduoke\services\facade\WechatService as Ws;
use think\App;

class Coupon extends WechatController
{
    /**
     * @var WechatCoupon
     */
    public $model;
    /**
     * @var mixed|string
     */
    private $user;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->user = Ws::getUser();
        $this->model = new WechatCoupon();
    }

    public function index()
    {
        $this->assign('title', "用户卡券");
        return $this->fetch("index");
    }

    public function getCouponShow($cid)
    {
        $coupon = $this->model->where(['id' => $cid])->find();
        if (!$coupon) {
            return $this->error("卡券不存在");
        }
        $coupon['end_text'] = $coupon['end_time'] == 0 ? "永不过期" : date("Y/m/d", $coupon['end_time']) . " 到期";

        return $this->success("ok", $coupon);
    }


    public function getCouponList()
    {
        $res = $this->model->getCouponUserList($this->user['id']);
        return $this->success($res['count'], $res['list']);
    }

    public function create()
    {
        $this->model->setName('带有地址和电话卡券')
            ->setRule("<ol><li>代码命名是件很头疼的事情，既要<code>语义化</code>又要<code>简洁</code>，最终目的为了代码的<code>可维护性</code></li><li><p>合适的命名会使代码读起来优雅流畅、耐人寻味，让你的代码如艺术品一般。比如：</p><ul><li><code>promise</code> - 异步请求数据时，“承诺”一定会回应（成功或失败），很有画面感</li><li><code>on/off</code> - jquery 中表示监听和销毁事件，言简意赅，方便易用</li><li><code>fork</code> - github 的 fork 操作，由“叉子”引申为复刻属于自己的分叉，形象生动</li></ul></li></ol>")
            ->setCheckUid(8)
            ->setPrice('98')->setUseDay('1', '2')
            ->setPlace("小龙坎火锅", "四川省成都市郫都区小龙坎火锅", "30.48604", "104.022461", "15002831754")
            ->setType(2)
            ->sendCoupon();
    }

}
